<template>
  <div class="my-container">
     <!-- 登录成功显示页面 -->
    <div class="header user-info" v-if="user">

      <!-- 用户基本信息 -->
      <div class="base-info">
        <!-- 左边头像 -->
        <div class="left">
          <van-image
           class="avatar"
            round
            width="100"
            height="100"
           :src="userInfo.photo"
          />
            <span class="name">{{ userInfo.name }}</span>
        </div>
      <!-- 右边编辑功能 -->
        <div class="right">
          <van-button type="default">编辑资料</van-button>
        </div>

      </div>

        <!-- 粉丝、关注 -->
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>

    </div>

    <!-- 未登录状态 -->
    <div class="header not-login"  v-else >
      <div class="login-btn" @click="$router.push('/login')">
         <img class="mobile-img" src="~@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 宫格导航 -->
      <van-grid class="grid-nav" :column-num="2"  clickable>
        <van-grid-item class="grid-item">
          <i slot="icon" class="toutiao toutiao-shoucang"></i>
          <span slot="text" >收藏</span>
        </van-grid-item>
        <van-grid-item class="grid-item">
            <i slot="icon" class="toutiao toutiao-lishi"></i>
          <span slot="text" >历史</span>
        </van-grid-item>
      </van-grid>
      <!-- 单元导航 -->
      <div class="cell">
      <van-cell title="消息通知" is-link />
      <van-cell title="小爱同学" is-link />
      <van-cell
        clickable
        v-if="user"
        title="退出登录"
        class="logout-cell"
        @click="onLogout()"
        />
      </div>
  </div>
</template>
<script>
// 引入vuex 里面的 mapState方法
import { mapState } from 'vuex'
// 引入接口
import { getUserInfoAPI } from '../../api/index.js'
export default {
  name: 'myPage',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    if (this.user) {
      this.getUserInfo()
    }
  },
  mounted () {},
  methods: {
    // 登录退出功能
    onLogout () {
      // 退出提示
      this.$dialog.confirm({
        message: '退出了喔!'
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
      // 确认退出:清除登录状态
    },
    // 登录后获得用户信息
    async getUserInfo () {
      try {
        const { data } = await getUserInfoAPI()
        // console.log(data)
        this.userInfo = data.data
        // console.log(this.userInfo)
      } catch (err) {
        this.$toast('获取数据失败,请稍后重试', err)
      }
    }
  }
}
</script>

<style scoped lang="less">
//设置顶部样式
  .my-container{
    .header{
      height: 361px;
      background: url('../../assets/banner.png');
      background-size: cover;
    }
     //未登录样式
  .not-login{
     display: flex;
     justify-content: center;
     align-items: center;
      .login-btn{
        display: flex;
        box-sizing: border-box;
        flex-direction:column;
        justify-content: center;
        align-items: center;
          .mobile-img {
            width: 132px;
            height: 132px;
            margin-bottom: 15px;
      }
          .text {
            font-size: 28px;
            color: #fff;
          }
      }
  }
  //登录后样式
  .user-info {
    //基础信息
     .base-info {
        height: 231px;
        padding: 76px 32px 23px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
      .left {
      display: flex;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        border: 4px solid #fff;
        margin-right: 23px;
      }
      .name {
        font-size: 30px;
        color: #fff;
      }
    }
  }
    //数据
      .data-stats {
       display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }

}

  //宫格导航
   .grid-nav {
    .grid-item {
      height: 141px;
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
    }
  }
}
 //cell单元格导航
 .cell{
  .logout-cell{
    text-align: center;
    color: red;
  }
 }
  }

</style>
